﻿using DevExpress.Charts.Native;
using DevExpress.XtraEditors;
using Shopfloormanagement;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Shopfloormanagement
{
    public partial class Step2 : DevExpress.XtraEditors.XtraForm
    {
        public Step2()
        {
            InitializeComponent();
        }
        int step1;
        int step2;
        int step3;
        int step4;
        int step5;
        int step6;
        int step7;
        int step8;
        int step9;
        private string ConvertImageToBase64(PictureEdit pic)//图片转换为Base64
        {
            if (pic.Image == null)
            {
                return null;
            }
            using (MemoryStream ms = new MemoryStream())
            {
                pic.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] imageBytes = ms.ToArray();
                return Convert.ToBase64String(imageBytes);

            }


        }
        private Image CovertBase64ToImage(string base64String)//Base64转化为图片
        {
            try
            {
                // 将 Base64 字符串解码为字节数组
                byte[] imageBytes = Convert.FromBase64String(base64String);

                // 使用 MemoryStream 将字节数组转换为 Image 对象
                using (MemoryStream ms = new MemoryStream(imageBytes))
                {
                    Image image = Image.FromStream(ms);
                    return image;
                }
            }
            catch (Exception ex)
            {
                
                // 处理异常（例如 Base64 字符串格式错误）
                throw new Exception("Base64 字符串转换失败: " + ex.Message);
            }
        }
        private void LoadA3step2()//加载信息、图片及判断是否过期
        {
            //获取选中A3的数据(IS/ISNOT)
            DataTable A3Step2 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step2", $"A3ReportNumberID = '{rootcause.A3number}'");

            if (A3Step2.Rows.Count > 0)
            {
                txtIswhat.Text = A3Step2.Rows[0][2].ToString();
                txtIswhere.Text = A3Step2.Rows[0][3].ToString();
                txtIswhen.Text = A3Step2.Rows[0][4].ToString();
                txtIshow.Text = A3Step2.Rows[0][5].ToString();
                txtIsnotwhat.Text = A3Step2.Rows[0][6].ToString();
                txtIsnotwhere.Text = A3Step2.Rows[0][7].ToString();
                txtIsnotwhen.Text = A3Step2.Rows[0][8].ToString();
                txtIsnohow.Text = A3Step2.Rows[0][9].ToString();
                //添加图片
                string picture1 = A3Step2.Rows[0][10].ToString();
                string picture2 = A3Step2.Rows[0][11].ToString();
                string picture3 = A3Step2.Rows[0][12].ToString();
                if (picture1 != "")
                {
                    Image pic1image = CovertBase64ToImage(A3Step2.Rows[0][10].ToString());
                    pic1.Image = pic1image;
                    pic1.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
                }
                if (picture2 != "")
                {
                    Image pic2image = CovertBase64ToImage(A3Step2.Rows[0][11].ToString());
                    pic2.Image = pic2image;
                    pic2.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;

                }
                if (picture3 != "")
                {
                    Image pic3image = CovertBase64ToImage(A3Step2.Rows[0][12].ToString());
                    pic3.Image = pic3image;
                    pic3.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
                }
                
                
            }
            else
            {
                return;

            }
            //获取选中A3Step1的数据(Duedate)
            DataTable A3Step1 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step1", $"A3ReportNumberID = '{rootcause.A3number}'");
            //超期图片变红
            object dbTimeObj = A3Step1.Rows[0]["Duedate"];//获取数据库中的日期数据
            DateTime dbTime;
            if (dbTimeObj != DBNull.Value && DateTime.TryParse(dbTimeObj.ToString(), out dbTime))
            {
                if (dbTime < DateTime.Now)
                {
                    bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step2 = 3", $"ID = '{rootcause.A3number}' and Step2 != 1");
                    //Load A3list 获得step2的值
                    DataTable A3listTemp = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_List", $"ID = '{rootcause.A3number}'");
                    //获取上方图片状态
                    step1 = (int)A3listTemp.Rows[0][6];
                    step2 = (int)A3listTemp.Rows[0][7];
                    step3 = (int)A3listTemp.Rows[0][8];
                    step4 = (int)A3listTemp.Rows[0][9];
                    step5 = (int)A3listTemp.Rows[0][10];
                    step6 = (int)A3listTemp.Rows[0][11];
                    step7 = (int)A3listTemp.Rows[0][12];
                    step8 = (int)A3listTemp.Rows[0][13];
                    step9 = (int)A3listTemp.Rows[0][14];
                    LoadPicture(step1, step2, step3, step4, step5, step6, step7, step8, step9);
                }
                else
                {
                    return;
                }

            }
        }
        private void LoadA3list()//获取上方图片状态
        {
            
            DataTable A3list = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_List", $"ID = '{rootcause.A3number}'");
                       
            step1 = (int)A3list.Rows[0][6];
            step2 = (int)A3list.Rows[0][7];
            step3 = (int)A3list.Rows[0][8];
            step4 = (int)A3list.Rows[0][9];
            step5 = (int)A3list.Rows[0][10];
            step6 = (int)A3list.Rows[0][11];
            step7 = (int)A3list.Rows[0][12];
            step8 = (int)A3list.Rows[0][13];
            step9 = (int)A3list.Rows[0][14];
            LoadPicture(step1, step2, step3, step4, step5, step6, step7, step8, step9);
        }
        private void Word()//加载进度下方文字
        {
            lblStep1.Text = "Problem\nStatement";
            lblStep2.Text = "Problem\nDescription";
            lblStep3.Text = "Interim\nContainment\nactions";
            lblStep4.Text = "Identify\nPossible\nCauses";
            lblStep5.Text = "Test all\nPossible\nCauses";
            lblStep6.Text = "Verify\nPotential\nRoot Cause";
            lblStep7.Text = "Confirmed\nRoot Cause";
            lblStep8.Text = "Permanent\nCorrective\nActions";
            lblStep9.Text = "Further\nActivities";

        }
        private void Step2_Load(object sender, EventArgs e)
        {
            //进度进度下方文字下方文字加载
            Word();
            //获取选中A3的数据(进程图片数据)
            LoadA3list();

            LoadA3step2();

            SetButtonEnable(this);
            palstep1.BackColor = Color.FromArgb(146, 208, 80);
            palstep2.BackColor = Color.FromArgb(146, 208, 80);
        }
        private void SetButtonEnable(Control control)//检索所有TextEdit控件是否有变化，如有变化则不显示Next
        {

            if (control is TextEdit)
            {
                control.TextChanged += TextEditTextChange;
            }
            else
            {
                var controlCollection = control.Controls;
                foreach (Control subControl in controlCollection)
                {
                    SetButtonEnable(subControl);
                }
            }


        }
        private void TextEditTextChange(object sender, EventArgs e)
        {

            btnNext.Visible = false;

        }//为SetButtonEnable制作的命令
        private void LoadPicture(int condition1, int condition2, int condition3, int condition4, int condition5, int condition6, int condition7, int condition8, int condition9)
        {
            switch (condition1)
            {
                case 1:
                    picstep1.Image = Properties.Resources.G1;
                    break;
                case 2:
                    picstep1.Image = Properties.Resources.B1;
                    break;
                case 3:
                    picstep1.Image = Properties.Resources.R1;
                    break;
                case 4:
                    picstep1.Image = Properties.Resources.H1;
                    break;
                default:
                    picstep1.Image = null;
                    break;
            }
            switch (condition2)
            {
                case 1:
                    picstep2.Image = Properties.Resources.G2;
                    break;
                case 2:
                    picstep2.Image = Properties.Resources.B2;
                    break;
                case 3:
                    picstep2.Image = Properties.Resources.R2;
                    break;
                case 4:
                    picstep2.Image = Properties.Resources.H2;
                    break;
                default:
                    picstep2.Image = null;
                    break;
            }
            switch (condition3)
            {
                case 1:
                    picstep3.Image = Properties.Resources.G3;
                    break;
                case 2:
                    picstep3.Image = Properties.Resources.B3;
                    break;
                case 3:
                    picstep3.Image = Properties.Resources.R3;
                    break;
                case 4:
                    picstep3.Image = Properties.Resources.H3;
                    break;
                default:
                    picstep3.Image = null;
                    break;
            }
            switch (condition4)
            {
                case 1:
                    picstep4.Image = Properties.Resources.G4;
                    break;
                case 2:
                    picstep4.Image = Properties.Resources.B4;
                    break;
                case 3:
                    picstep4.Image = Properties.Resources.R4;
                    break;
                case 4:
                    picstep4.Image = Properties.Resources.H4;
                    break;
                default:
                    picstep4.Image = null;
                    break;
            }
            switch (condition5)
            {
                case 1:
                    picstep5.Image = Properties.Resources.G5;
                    break;
                case 2:
                    picstep5.Image = Properties.Resources.B5;
                    break;
                case 3:
                    picstep5.Image = Properties.Resources.R5;
                    break;
                case 4:
                    picstep5.Image = Properties.Resources.H5;
                    break;
                default:
                    picstep5.Image = null;
                    break;
            }
            switch (condition6)
            {
                case 1:
                    picstep6.Image = Properties.Resources.G6;
                    break;
                case 2:
                    picstep6.Image = Properties.Resources.B6;
                    break;
                case 3:
                    picstep6.Image = Properties.Resources.R6;
                    break;
                case 4:
                    picstep6.Image = Properties.Resources.H6;
                    break;
                default:
                    picstep6.Image = null;
                    break;
            }
            switch (condition7)
            {
                case 1:
                    picstep7.Image = Properties.Resources.G7;
                    break;
                case 2:
                    picstep7.Image = Properties.Resources.B7;
                    break;
                case 3:
                    picstep7.Image = Properties.Resources.R7;
                    break;
                case 4:
                    picstep7.Image = Properties.Resources.H7;
                    break;
                default:
                    picstep7.Image = null;
                    break;
            }
            switch (condition8)
            {
                case 1:
                    picstep8.Image = Properties.Resources.G8;
                    break;
                case 2:
                    picstep8.Image = Properties.Resources.B8;
                    break;
                case 3:
                    picstep8.Image = Properties.Resources.R8;
                    break;
                case 4:
                    picstep8.Image = Properties.Resources.H8;
                    break;
                default:
                    picstep8.Image = null;
                    break;
            }
            switch (condition9)
            {
                case 1:
                    picstep9.Image = Properties.Resources.G9;
                    break;
                case 2:
                    picstep9.Image = Properties.Resources.B9;
                    break;
                case 3:
                    picstep9.Image = Properties.Resources.R9;
                    break;
                case 4:
                    picstep9.Image = Properties.Resources.H9;
                    break;
                default:
                    picstep9.Image = null;
                    break;
            }

        }
        private void btnBack_Click(object sender, EventArgs e)
        {
            Step1 frm = new Step1();
            frm.Show();
            this.Close();
        }
        private void btnNext_Click(object sender, EventArgs e)//判断IS不能为空，才能进行下一步
        {
            if (txtIswhat.Text == "")
            {
                MessageBox.Show("IS What 不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;

            }
            else if (txtIswhen.Text == "")
            {
                MessageBox.Show("IS When 不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (txtIswhere.Text == "")
            {
                MessageBox.Show("IS Where 不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (txtIshow.Text == "")
            {
                MessageBox.Show("IS How Many 不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {

                bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step2 = 1", $"ID = '{rootcause.A3number}'");

                if (reslist)
                {

                    Step3 frm = new Step3();
                    frm.Show();
                    this.Hide();
                }

            }
            

        }
        private void btnSave_Click(object sender, EventArgs e)
        {

            string pic1_Str = ConvertImageToBase64(pic1);
            string pic2_Str = ConvertImageToBase64(pic2);
            string pic3_Str = ConvertImageToBase64(pic3);
            
            DataTable A3Step2 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step2", $"A3ReportNumberID = '{rootcause.A3number}'");
            if (A3Step2.Rows.Count > 0)
            {
                string contentstep2 = $"IsWhat = '{txtIswhat.Text}',IsWhere = '{txtIswhere.Text}',IsWhen = '{txtIswhen.Text}',IsHow = '{txtIshow.Text}'," +
                    $"IsnotWhat = '{txtIsnotwhat.Text}',IsnotWhere = '{txtIsnotwhere.Text}',IsnotWhen = '{txtIsnotwhen.Text}',IsnotHow = '{txtIsnohow.Text}'," +
                    $"Picture1 = '{pic1_Str}',Picture2 = '{pic2_Str}',Picture3 = '{pic3_Str}'";
                bool resstep2 = LSQL.SQL.Update(SystemInfo.SQL, "A3_Step2", contentstep2, $"A3ReportNumberID = '{rootcause.A3number}'");
                bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step2 = 2", $"ID = '{rootcause.A3number}'");
                if (resstep2 == true && reslist == true)
                {

                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Step2_Load(sender, e);
                    btnNext.Visible = true;
                }
                else
                {
                    MessageBox.Show("更新失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }

            }
            else
            {
                string column = "[A3ReportNumberID],[IsWhat],[IsWhere],[IsWhen],[IsHow],[IsnotWhat],[IsnotWhere],[IsnotWhen],[IsnotHow],[Picture1],[Picture2],[Picture3]";
                string content = $"'{rootcause.A3number}','{txtIswhat.Text}','{txtIswhere.Text}','{txtIswhen.Text}','{txtIshow.Text}','{txtIsnotwhat.Text}','{txtIsnotwhere.Text}'," +
                    $"'{txtIsnotwhen.Text}','{txtIsnohow.Text}','{pic1_Str}','{pic2_Str}','{pic3_Str}'";
                int res = LSQL.SQL.Insert(SystemInfo.SQL, "A3_Step2", column, content);
                if (res >= 1)
                {

                    bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step2 = 2", $"ID = '{rootcause.A3number}'");
                    MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Step2_Load(sender, e);
                    btnNext.Visible = true;
                }
                else
                {
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }
            }
        }
        private void btnClosed_Click(object sender, EventArgs e)
        {
            // 弹出确认对话框，询问是否关闭
            DialogResult result = MessageBox.Show("是否确定关闭？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                // 用户选择“是”，执行关闭操作
                this.Close(); // 关闭当前窗体
            }
            else
            {
                // 用户选择“否”，返回，不做任何操作
                return;
            }
        }
        //点击图片跳转功能
        private void picstep1_Click(object sender, EventArgs e)
        {
            if (step1 == 4)
            {
                return;
            }
            else
            {
                Step1 frm = new Step1();
                this.Close();
                frm.Show();
            }
        }
        private void picstep2_Click(object sender, EventArgs e)
        {
            if (step2 == 4)
            {
                return;
            }
            else
            {
                Step2 frm = new Step2();
                this.Close();
                frm.Show();
            }
        }
        private void picstep3_Click(object sender, EventArgs e)
        {
            if (step3 == 4)
            {
                return;
            }
            else
            {
                Step3 frm = new Step3();
                this.Close();
                frm.Show();
            }
        }
        private void picstep4_Click(object sender, EventArgs e)
        {
            if (step4 == 4)
            {
                return;
            }
            else
            {
                Step4 frm = new Step4();
                this.Close();
                frm.Show();
            }
        }
        private void picstep5_Click(object sender, EventArgs e)
        {
            if (step5 == 4)
            {
                return;
            }
            else
            {
                Step5 frm = new Step5();
                this.Close();
                frm.Show();
            }
        }
        private void picstep6_Click(object sender, EventArgs e)
        {

        }
        private void picstep7_Click(object sender, EventArgs e)
        {
            if (step7 == 4)
            {
                return;
            }
            else
            {
                Step7 frm = new Step7();
                this.Close();
                frm.Show();
            }
        }
        private void picstep8_Click(object sender, EventArgs e)
        {
            if (step8 == 4)
            {
                return;
            }
            else
            {
                Step8 frm = new Step8();
                this.Close();
                frm.Show();
            }
        }
        private void picstep9_Click(object sender, EventArgs e)
        {
            if (step9 == 4)
            {
                return;
            }
            else
            {
                Step9 frm = new Step9();
                this.Close();
                frm.Show();
            }
        }
    }
}